package com.wiwf.model.security.service.impl;

import com.gitee.fastmybatis.core.mapper.CrudMapper;
import com.wiwf.model.security.service.SysTokenService;
import com.wiwf.model.security.service.SysUserService;
import com.wiwf.model.mapper.admin.bean.SysMenuEntity;
import com.wiwf.model.mapper.admin.bean.SysUserEntity;
import com.wiwf.model.bean.SysUserVoto;
import com.wiwf.model.mapper.admin.dao.SysUserMapper;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.wiwf.model.bean.SysUserBean;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kehui
 */
@Service
public class SysUserServiceImpl extends FastmbatisServiceImpl<SysUserEntity, Integer> implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysTokenService tokenService;

	@Resource
	private PasswordEncoder passwordEncoder;

    @Override
    public CrudMapper<SysUserEntity, Integer> getMapper() {
        return sysUserMapper;
    }

    @Override
    public SysUserBean getUser(String username) {
        return getUserInfo(getDefUserInfo(username));
    }
    @Override
    public SysUserBean getUserTk(HttpServletResponse response, String username) {
        return getUserInfoTk(response, getDefUserInfo(username));
    }
    @Override
    public SysUserBean getUser(HttpServletResponse response, String username, String password) {
    	SysUserBean user = getUserInfoTk(response, getDefUserInfo(username));
    	
    	String pwd = user.getPassword();
    	if(!ObjectUtils.isEmpty(pwd) && passwordEncoder.matches(password, pwd)) {
    		return user;
    	}
    	
    	return null;
    }
    @Override
    public SysUserBean getUser(HttpServletResponse response, String username, String password, String unitCode) {
        SysUserBean user = getUserInfoTk(response, getUnitUserInfo(username, unitCode));

        String pwd = user.getPassword();
        if(!ObjectUtils.isEmpty(pwd) && passwordEncoder.matches(password, pwd)) {
            return user;
        }

        return null;
    }

    /**
     * 获取用户信息 
     * @param username
     * @return
     */
    private SysUserVoto getDefUserInfo(String username){
        return sysUserMapper.getUserInfo(username);
    }
    /**
     * 获取用户信息 
     * @param username
     * @param unitCode
     * @return
     */
    private SysUserVoto getUnitUserInfo(String username, String unitCode){
        return sysUserMapper.getUnitUserInfo(username, unitCode);
    }
    
    private SysUserBean getUserInfo(SysUserVoto userInfo) {
        if(ObjectUtils.isEmpty(userInfo)) {
            return new SysUserBean();
        }
        List<String> authorities = new ArrayList<>();

        authorities.add("ROLE_" + userInfo.getRoleCode());
        List<SysMenuEntity> menus = userInfo.getMenus();
        for (SysMenuEntity menu : menus) {
            String menuCode = menu.getMenuCode();
            authorities.add(menuCode);
        }

        SysUserBean sysUserBean = new SysUserBean(userInfo);
        sysUserBean.setAuthorities(authorities);

        return sysUserBean;
    }
    private SysUserBean getUserInfoTk(HttpServletResponse response, SysUserVoto userInfo) {
        if(ObjectUtils.isEmpty(userInfo)) {
            return new SysUserBean();
        }
        List<String> authorities = new ArrayList<>();

        authorities.add("ROLE_" + userInfo.getRoleCode());
        List<SysMenuEntity> menus = userInfo.getMenus();
        for (SysMenuEntity menu : menus) {
            String menuCode = menu.getMenuCode();
            authorities.add(menuCode);
        }

        // 生成token
        String token = tokenService.createToken();
        // 存储token
     // System.out.println("\n\n============");
     // System.out.println(JSONObject.toJSONString(userInfo));
        tokenService.addTokenUser(response, token, userInfo);

        SysUserBean sysUserBean = new SysUserBean(userInfo);
        sysUserBean.setAuthorities(authorities);
        sysUserBean.setToken(token);

        return sysUserBean;
    }

}